#ifndef __MESSAGECENTER_H__
#define __MESSAGECENTER_H__

#include <pthread.h>
#include <list>
#include <map>

class Message;
class MessageObserver;
class MessagePoster;

//
// Observers is a map of NotificationName -> list<listening threads>
// Messages is a map of thread -> list of pending messages
//
class MessageCenter
{
  private:
  	//
  	// Private variables.
  	//
	pthread_mutex_t _observersMutex, _messagesMutex;
	pthread_cond_t _postCondition;
	std::map<std::string, std::list<MessageObserver *> > _observers;
	std::map<pthread_t, std::list<MessagePoster *> > _messages;

	//
	// Private methods.
	//
	MessageCenter();
	
	void ProcessMessages(std::list<MessagePoster *> &messages);
	void ProcessMessage(MessagePoster *poster);

  public:
	static MessageCenter &SharedMessageCenter();

	void AddObserver(std::string name, void (*callback)(Message *, void *), void *userInfo);
	void RemoveObserver(std::string name);

	void PostMessage(Message msg);

	void WaitForMessage();
	void WaitForMessage(long mstimeout);
};

#endif /* __MESSAGECENTER_H__ */

